package com.shijiangdiya.config;

import com.alibaba.fastjson.JSONObject;
import com.shijiangdiya.common.ResultEnum;
import lombok.Getter;
import lombok.Setter;

/**
 * @author hujiangdi
 * @date 2021/9/17 16:14
 * @des:
 */
@Getter
@Setter
public class Response {
    /**
     * 响应码
     */
    private String code;

    /**
     * 响应消息
     */
    private String msg;

    /**
     * 响应结果
     */
    private Object result;

    /**
     * 成功
     *
     * @return
     */
    public static Response success() {
        return new Response(ResultEnum.SUCCESS.getCode(),ResultEnum.SUCCESS.getValue(),null);
    }
    /**
     * 成功
     *
     * @return
     */
    public static Response success(String msg) {
        return new Response(ResultEnum.SUCCESS.getCode(),msg,null);
    }
    public Response() {
    }
    public Response(String code, String msg, Object result) {
        this.code = code;
        this.msg = msg;
        this.result = result;
    }

    /**
     * 成功
     * @param data
     * @return
     */
    public static Response success(Object data) {
        Response rb = new Response();
        rb.setCode(ResultEnum.SUCCESS.getCode());
        rb.setMsg(ResultEnum.SUCCESS.getValue());
        rb.setResult(data);
        return rb;
    }

    /**
     * 失败
     */
    public static Response error(String code, String message) {
        Response rb = new Response();
        rb.setCode(code);
        rb.setMsg(message);
        rb.setResult(null);
        return rb;
    }

    /**
     * 失败
     */
    public static Response error(String message) {
        Response rb = new Response();
        rb.setCode(ResultEnum.FAILED.getCode());
        rb.setMsg(message);
        rb.setResult(null);
        return rb;
    }

    @Override
    public String toString() {
        return JSONObject.toJSONString(this);
    }
}
